package cn.csxw.openchina.ui.view;

import android.content.Context;
import android.graphics.PointF;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Created by Administrator on 2017/1/13 0013.
 * 用子ViewPager解决事件分发冲突机制，子ViewPager可滑动，
 * 父ViewPager不可滑动
 */

public class ChildViewPager extends ViewPager {
    public ChildViewPager(Context context) {
        this(context, null);
    }

    public ChildViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    /**
     * 触摸时按下的点
     **/
    PointF downP = new PointF();
    /**
     * 触摸时当前的点
     **/

    PointF curP = new PointF();

    OnSingleTouchListener onSingleTouchListener;

   /* @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }*/

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        curP.x = ev.getX();
        curP.y = ev.getY();
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            downP.x = ev.getX();
            downP.y = ev.getY();
            getParent().requestDisallowInterceptTouchEvent(true);
        }

        if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            getParent().requestDisallowInterceptTouchEvent(true);
        }
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            if (downP.x == curP.x && downP.y == curP.y) {
                //回调单击事件的方法
                onSingleTouch();
                return true;
              /*  return true;*/
            }
        }
        return super.onTouchEvent(ev);
    }


    public void onSingleTouch() {
        if (onSingleTouchListener != null) {
            onSingleTouchListener.onSingleTouch();
        }
    }

    public interface OnSingleTouchListener {
        public void onSingleTouch();
    }

    //点击事件的接口
    public void setOnSingleTouchListener(OnSingleTouchListener onSingleTouchListener) {
        this.onSingleTouchListener = onSingleTouchListener;
    }

}
